package edu.bw.service.impl;

import cn.hutool.core.bean.BeanUtil;
import edu.bw.dao.UserDao;
import edu.bw.dao.impl.UserDaoImpl;
import edu.bw.dto.InsertUserForm;
import edu.bw.dto.SelectUserByConditionForm;
import edu.bw.dto.UpdateUserForm;
import edu.bw.pojo.User;
import edu.bw.service.UserService;
import edu.bw.utils.PageUtils;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

public class UserServiceImpl implements UserService {
        UserDao userDao=new UserDaoImpl();

    /**
     *
     * @param user
     * @return
     */
    @Override
    public Integer userlogin(User user) {
        return userDao.userLogin(user);
    }

    @Override
    public PageUtils selectUserByCondition(SelectUserByConditionForm selectCondition) {
        List<Map<String,Object>> selectUserByCondition = userDao.selectUserByCondition(selectCondition);
        Long mount=userDao.selectUserByConditionCount(selectCondition);
        int pageIndex=(selectCondition.getPage()-1)*selectCondition.getLength();
        return new PageUtils(selectUserByCondition,mount,pageIndex,selectCondition.getLength());
    }

    @Override

    public Integer insert(InsertUserForm insertUserForm) {
        //User user = new User();
        //user.setUsername(insertUserForm.getUsername());
        //user.setPassword(insertUserForm.getPassword());
        //....
        User user = new User();
        BeanUtil.copyProperties(insertUserForm, user);
        return userDao.insert(user);
    }

    @Override
    public List<Map<String, Object>> selectById(Integer userId) {
        return userDao.selectById(userId);
    }

    @Override
    public Integer update(UpdateUserForm updateUserForm) {
        //将UpdateUserForm=>User
        User user = new User();
        BeanUtil.copyProperties(updateUserForm, user);
        user.setId(updateUserForm.getUserId());
        return userDao.update(user);
    }

    @Override
    public Integer deleteUserByIds(List<Integer> ids) {
        return userDao.deleteUserByIds(ids);
    }
}
